Skip to content

fix(api): preserve module edits during version refresh - #4059

Open
mvanhorn wants to merge 2 commits into
terrakube-io:mainfrom
mvanhorn:fix/4053-module-refresh-lost-update
Open

mvanhorn wants to merge 2 commits into
terrakube-io:mainfrom
mvanhorn:fix/4053-module-refresh-lost-update

Conversation

@mvanhorn

@mvanhorn mvanhorn commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

What changed and why?

The API can accept an edit to a newly created module's description, then return the old description once a version recalculation runs. On main the refresh worker no longer holds a Module across ls-remote (loadTarget returns a ModuleRefreshTarget, and ModuleRefreshTransactions.recordSuccess takes the row lock through markRefreshed before it loads the Module). The stale overwrite comes from ModuleVersionManageHook, which recalculates POSTCOMMIT outside a transaction, or from any caller holding an older snapshot: ModuleLatestVersion saved that snapshot, which wrote its old description over the edit.

Version recalculation now updates only latestVersion by module ID with a targeted UPDATE, and leaves the Module entity untouched so a managed instance is not dirtied and flushed at commit. In recordSuccess, not saving the entity also keeps refreshes from moving updatedDate/updatedBy. The integration test commits a description edit after loading an older snapshot, then checks that both the edit and the newly discovered version are present after recalculation.

Fixes #4053

Testing

mvn -B -q -pl api -Dtest=ModuleLatestVersionTest -Dsurefire.failIfNoSpecifiedTests=false test passes locally on 9758ed1 (3 tests). ModuleRefreshTransactionsIntegrationTest runs in CI.

Visual changes (if applicable)

No user-visible surface changes in this PR, so there is nothing to show.

Version recalculation now updates only `latestVersion` by module ID. The
integration test commits a description edit after loading the refresh
snapshot, then checks that both the edit and the newly discovered
version are present after recalculation.

Fixes terrakube-io#4053
@github-actions github-actions Bot added terrakube-api area/scheduler Touches job scheduling, queueing or locking author/contributor Authored by a returning contributor priority/high Review soon: high-risk area, bug fix or security update size/M 50-249 lines changed type/fix Bug fix labels Nov 29, 2026

@Macbet Macbet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! The core change (switching ModuleLatestVersion to a targeted @Transactional @Modifying UPDATE and not mutating the Module entity) looks good and CI is green.

A few small things to clean up before approving and merging:

  1. Align comments with main's refresh flow
    On main, ModuleRefreshWorker no longer holds a Module entity across ls-remote (loadTarget returns a ModuleRefreshTarget DTO, and ModuleRefreshTransactions.recordSuccess acquires the row lock via markRefreshed before loading Module). The detached/stale entity overwrite on main happens via ModuleVersionManageHook (POSTCOMMIT, outside a transaction) or any caller holding a stale snapshot, while in recordSuccess avoiding save(module) keeps refreshes from bumping updatedDate/updatedBy.

    • Please update the comments in ModuleRefreshTransactionsIntegrationTest#refreshKeepsADescriptionEditedWhileItRuns (and the PR description) so they reflect main rather than the older 2.34.x ModuleRefreshJob flow.
  2. Update now-outdated comments in the modified files

    • ModuleRepository.java (comment above markRefreshed): remove (Module has @DynamicUpdate, so saving it later does not write these columns back) since recordSuccess no longer saves the entity.
    • ModuleRefreshTransactionsIntegrationTest.java (successThatChangesLatestVersionKeepsTheComputedNextRefresh): update which ModuleLatestVersion saves through the entity since it now uses a targeted UPDATE.
  3. Note why module.setLatestVersion(...) is omitted
    In ModuleLatestVersion.java, please mention in the comment that leaving the entity untouched is intentional so a managed Module isn't dirtied and flushed back at transaction commit.

  4. Clean up the ## Testing section in the PR description
    It currently contains contradictory lines ("timed out after 599 seconds", "all 20 pass", "did not run here").

(Optional nits): adding verifyNoMoreInteractions(moduleRepository) (or verify(moduleRepository, never()).save(any())) in ModuleLatestVersionTest and passing e to log.error(...) in ModuleLatestVersion's catch block would also be nice to have.

@mvanhorn

Copy link
Copy Markdown
Contributor Author

@Macbet thanks for the review, pushed 9758ed1 for all four points plus both nits.

  1. The comments in refreshKeepsADescriptionEditedWhileItRuns and the PR description now describe main: the stale snapshot comes from ModuleVersionManageHook (POSTCOMMIT) or another caller holding an older Module, while the refresh worker loads a ModuleRefreshTarget and recordSuccess takes the row lock via markRefreshed first.
  2. The @DynamicUpdate parenthetical above markRefreshed is gone, and the comment in successThatChangesLatestVersionKeepsTheComputedNextRefresh now says ModuleLatestVersion writes through a targeted UPDATE.
  3. ModuleLatestVersion notes that skipping module.setLatestVersion(...) is intentional so a managed Module is not dirtied and flushed at commit, and the catch now passes e to log.error.
  4. The Testing section is rewritten. ModuleLatestVersionTest also checks verify(moduleRepository, never()).save(any()) in both cases, and it passes locally on 9758ed1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/scheduler Touches job scheduling, queueing or locking author/contributor Authored by a returning contributor priority/high Review soon: high-risk area, bug fix or security update size/M 50-249 lines changed terrakube-api type/fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Module refresh right after creation undoes an update made while it runs

2 participants